<?php
namespace app\admin\controller;
use \think\Request;
use \think\Validate;
use \think\Session;
use \think\Controller;
use app\admin\model\Manager;
// use app\admin\controller\Common;
class Index extends Controller
{

	/*
	后台管理员登录
	 */
    public function index()
    {
    
    	if (Request::instance()->isPut()) {

    		$data = Request::instance()->put();
    		$validate = new Validate([  
	            'username' => 'require|min:1',  
	            'password' => 'require|min:32',  
        	]);  
  
	        $data=[  
	            'username'=>$data['username'],  
	            'password'=>md5($data['password']),  
	        ];  
  
	        if(!$validate->check($data)){  
	            $this->error($validate->getError());  
	        }
    		
    		//查询当前用户名和密码是否存在数据
    		$manager = new Manager;
    		$result = $manager->isAllow($data);
    		if ($result) {
                // var_dump($result);
                Session::set('username',$result['username']);
                Session::set('id',$result['id']);
                // echo Session::get('username');
                // echo Session::get('id');
    			return $this->fetch();
    		}else{
    			return $this->error('登录失败');
    		}	
    	}else{
    		return $this->fetch();
    	}
    	
        
    }
}
